Numeric text box means, a Text Box which can only accepts numeric values.

Code to implement a numeric text box.
Code should be written on textbox keypress event.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar) == false && e.KeyChar != '.' && e.KeyChar!='\b')
//checking whether pressed key is number, decimal or backspace.
//If false then we will set Handled variable of KeyPressEventArgs to true.
//By doing this system will get a message that character of keypressed
//is executed and hence aplpa numeric character will not be displayed
//except decimal.
e.Handled=true;
else if (e.KeyChar == '.')
if (textBox1.Text.Contains('.'))
e.Handled=true;
//checking whether decimal key is pressed or not, if true then checking
//whether decimal is already present or not, as we can have only
//one decimal point in a text box.
}
Leave Comment
2 Comments